package com.mdd.front.controller;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.UserCommemorate;
import com.mdd.common.entity.UserFriend;
import com.mdd.common.entity.UserFriendGroup;
import com.mdd.common.entity.UserFriendRequest;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.LikeFrontThreadLocal;
import com.mdd.front.service.IFriendService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.vo.friend.FriendInfoVo;
import com.mdd.front.vo.friend.UserCommemorateVo;
import com.mdd.front.vo.user.UserLableGroupVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/friend")
@Api(tags = "好友相关API")
public class FriendController {

	@Autowired
	private IFriendService iFriendService;
	
	@PostMapping("/importCommunicateUser")
	@ApiOperation(value = "导入用户通讯录")
	public AjaxResult<Object> importCommunicateUser(@RequestBody List<UserFriend> list) {
		if(CollectionUtils.isEmpty(list)) {
			return AjaxResult.failed("数据不存在");
		}
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.importCommunicateUser(list,userId);
	}
	
	@GetMapping("/friendList")
	@ApiOperation(value = "好友列表")
	public AjaxResult<List<UserFriend>> friendList(String keyword) {
		Integer userId = LikeFrontThreadLocal.getUserId();
		System.out.println("好友列表参数：userId="+userId+",参数keyword="+keyword);
		List<UserFriend> friendList = iFriendService.friendList(userId,keyword);
		return AjaxResult.success(friendList); 
	}
	
	@GetMapping("/communicateList")
	@ApiOperation(value = "通讯录列表")
	public AjaxResult<PageResult<UserFriend>> communicateList(@Validated PageValidate pageValidate,String keyword) {
		Integer userId = LikeFrontThreadLocal.getUserId();
		PageResult<UserFriend> friendList = iFriendService.communicateList(userId,pageValidate,keyword);
		return AjaxResult.success(friendList); 
	}
	
	@PostMapping("/deleteCommunicate")
	@ApiOperation(value = "删除通讯录")
	public AjaxResult<Object> deleteCommunicate(@RequestBody UserFriend userFfiend) {
		Assert.notNull(userFfiend, "请选择用户");
		Assert.notNull(userFfiend.getId(), "请选择用户");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return  iFriendService.deleteCommunicate(userId,userFfiend.getId());
	}
	
	
	
	@PostMapping("/addCommunicateUser")
	@ApiOperation(value = "添加通讯录用户")
	public AjaxResult<Object> addCommunicateUser(@RequestBody UserFriend userFfiend) {
		Assert.notNull(userFfiend, "请输入用户信息");
		Assert.notNull(userFfiend.getName(), "请输入用户姓名");
		Assert.notNull(userFfiend.getPhone(), "请输入用户手机号码");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.addCommunicateUser(userFfiend,userId);
	}
	
	@PostMapping("/updCommunicateUser")
	@ApiOperation(value = "修改通讯录用户")
	public AjaxResult<Object> updCommunicateUser(@RequestBody UserFriend userFfiend) {
		Assert.notNull(userFfiend, "请输入用户信息");
		Assert.notNull(userFfiend.getId(), "请选择用户");
		Assert.notNull(userFfiend.getName(), "请输入用户姓名");
		Assert.notNull(userFfiend.getPhone(), "请输入用户手机号码");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.updCommunicateUser(userFfiend,userId);
	}
	
	@NotLogin
	@GetMapping("/friendRequestList")
	@ApiOperation(value = "好友请求列表")
	public AjaxResult<PageResult<FriendInfoVo>> friendRequestList(@Validated PageValidate pageValidate) {
		Integer userId = LikeFrontThreadLocal.getUserId();
		PageResult<FriendInfoVo> friendList = iFriendService.friendRequestList(userId,pageValidate);
		return AjaxResult.success(friendList); 
	}
	
	@PostMapping("/addUserGroup")
	@ApiOperation(value = "添加标签分组")
	public AjaxResult<Object> addUserGroup(@RequestBody UserFriendGroup userFriendGroup) {
		Assert.notNull(userFriendGroup, "请输入分组名");
		Assert.notNull(userFriendGroup.getGroupName(), "请输入分组名");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.addUserGroup(userFriendGroup,userId);
	}
	
	@PostMapping("/updUserGroup")
	@ApiOperation(value = "修改标签分组")
	public AjaxResult<Object> updUserGroup(@RequestBody UserFriendGroup userFriendGroup) {
		Assert.notNull(userFriendGroup, "请输入分组名");
		Assert.notNull(userFriendGroup.getId(), "请输入分组名");
		Assert.notNull(userFriendGroup.getGroupName(), "请输入分组名");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.updUserGroup(userFriendGroup,userId);
	}
	
	@PostMapping("/setUserGroup")
	@ApiOperation(value = "设置用户分组/特别关注")
	public AjaxResult<Object> setUserGroup(@RequestBody UserFriendGroup userFriendGroup) {
		Assert.notNull(userFriendGroup, "请选择分组");
		Assert.notNull(userFriendGroup.getToUserId(), "请选择用户");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.setUserGroup(userFriendGroup,userId);
	}
	
	@PostMapping("/removeFollow")
	@ApiOperation(value = "移除特别关注")
	public AjaxResult<Object> removeFollow(@RequestBody UserFriendGroup userFriendGroup) {
		Assert.notNull(userFriendGroup, "请选择用户");
		Assert.notNull(userFriendGroup.getToUserId(), "请选择用户");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.removeFollow(userFriendGroup.getToUserId(),userId);
	}
	
	@GetMapping("/specialFriendList")
	@ApiOperation(value = "特别关注的好友列表")
	public AjaxResult<PageResult<UserFriend>> specialFriendList(@Validated PageValidate pageValidate) {
		Integer userId = LikeFrontThreadLocal.getUserId();
		PageResult<UserFriend> friendList = iFriendService.specialFriendList(userId,pageValidate);
		return AjaxResult.success(friendList); 
	}
	
	@GetMapping("/lableGroupList")
	@ApiOperation(value = "标签分组列表")
	public AjaxResult<List<UserLableGroupVo>> lableGroupList() {
		Integer userId = LikeFrontThreadLocal.getUserId();
		List<UserLableGroupVo> friendList = iFriendService.lableGroupList(userId);
		return AjaxResult.success(friendList); 
	}
	
	@GetMapping("/userBlacklist")
	@ApiOperation(value = "黑名单列表")
	public AjaxResult<PageResult<FriendInfoVo>> userBlacklist(@Validated PageValidate pageValidate) {
		Integer userId = LikeFrontThreadLocal.getUserId();
		return AjaxResult.success(iFriendService.userBlacklist(userId, pageValidate)); 
	}
	
	@PostMapping("/blockOrUnBlock")
	@ApiOperation(value = "拉黑或者取消拉黑")
	public AjaxResult<Object> blockOrUnBlock(@RequestBody UserFriendGroup userFriendGroup) {
		Assert.notNull(userFriendGroup, "请选择用户");
		Assert.notNull(userFriendGroup.getUserId(), "请选择用户");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.blockOrUnBlock(userId, userFriendGroup.getUserId()); 
	}
	
	@PostMapping("/applyforFriend")
	@ApiOperation(value = "好友申请")
	public AjaxResult<Object> applyforFriend(@RequestBody UserFriendRequest userFriendRequest) {
		Assert.notNull(userFriendRequest, "请选择用户");
		Assert.notNull(userFriendRequest.getUserId(), "请选择用户");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.applyforFriend(userId, userFriendRequest.getUserId()); 
	}
	
	
	@PostMapping("/applyforFriendHandle")
	@ApiOperation(value = "好友申请处理")
	public AjaxResult<Object> applyforFriendHandle(@RequestBody UserFriendRequest userFriendRequest) {
		Assert.notNull(userFriendRequest, "请选择处理类型");
		Assert.notNull(userFriendRequest.getId(), "请选择处理类型");
		Assert.notNull(userFriendRequest.getType(), "请选择处理类型");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.applyforFriendHandle(userId, userFriendRequest); 
	}
	
	@PostMapping("/delFriend")
	@ApiOperation(value = "删除好友")
	public AjaxResult<Object> delFriend(@RequestBody UserFriendRequest userFriendRequest) {
		Assert.notNull(userFriendRequest, "请选择处理类型");
		Assert.notNull(userFriendRequest.getUserId(), "请选择要删除的好友");
		Integer userId = LikeFrontThreadLocal.getUserId();
		return iFriendService.delFriend(userId, userFriendRequest.getUserId()); 
	}
	
	@GetMapping("/friendDetail")
	@ApiOperation(value = "好友详情")
	public AjaxResult<UserFriend> friendDetail(Integer userId) {
		Assert.notNull(userId, "请选择好友");
		Integer currUserId = LikeFrontThreadLocal.getUserId();
		return AjaxResult.success(iFriendService.friendDetail(currUserId,userId)); 
	}
	
	@PostMapping("/setCommemorate")
	@ApiOperation(value = "设置纪念日")
	public AjaxResult<Object> setCommemorate(@RequestBody UserCommemorateVo vo) {
		Assert.notNull(vo, "请选择好友");
		Assert.notNull(vo.getUid(), "请选择好友");
		Assert.notNull(vo.getTimeShow(), "请选择时间");
		Assert.notNull(vo.getName(), "请输入名称");
		Integer currUserId = LikeFrontThreadLocal.getUserId();
		UserCommemorate userCommemorate = new UserCommemorate(); 
		userCommemorate.setUid(vo.getUid());
		userCommemorate.setName(vo.getName());
		userCommemorate.setTime(TimeUtils.parse("yyyy-MM-dd", vo.getTimeShow()));
		return iFriendService.setCommemorate(currUserId,userCommemorate); 
	}
	
	@GetMapping("/userCommemorateList")
	@ApiOperation(value = "用户纪念日列表")
	public AjaxResult<PageResult<UserCommemorateVo>> userCommemorateList(@Validated PageValidate pageValidate,Integer userId) {
		Integer currUserId = LikeFrontThreadLocal.getUserId();
		return AjaxResult.success(iFriendService.userCommemorateList(currUserId,userId, pageValidate)); 
	}
	
	@PostMapping("/commemorateRemind")
	@ApiOperation(value = "设置纪念日提醒")
	public AjaxResult<Object> commemorateRemind(@RequestBody UserCommemorate userCommemorate) {
		Assert.notNull(userCommemorate, "请选择纪念日");
		Assert.notNull(userCommemorate.getId(), "请选择好友");
		Integer currUserId = LikeFrontThreadLocal.getUserId();
		return iFriendService.commemorateRemind(currUserId,userCommemorate.getId()); 
	}
	
	public static void main(String[] args) {
		System.out.println(TimeUtils.millisecondToDate(1722913836000L));
	}
}
